package cn.openread.demo.video;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * 用途：一切为了业务
 * <p>
 * Created by simon on 2020-02-02.
 */
public class Main {

    public static void main(String[] args) {

        String path ="/Users/mac/Downloads";


        for (String s : new File(path).list()) {
            System.out.println(s);
        }
    }

    public static  void CombineFile(String path,String tar) throws Exception {
        try {
            File dirFile = new File(path);
            FileInputStream fis;
            FileOutputStream fos = new FileOutputStream(tar);
            byte buffer[] = new byte[1024 * 1024 * 2];//一次读取2M数据，将读取到的数据保存到byte字节数组中
            int len;
            if(dirFile.isDirectory()) { //判断file是否为目录
                String[] fileNames = dirFile.list();
                Arrays.sort(fileNames, new StringComparator());//实现目录自定义排序
                for (int i = 0;i<fileNames.length ;i++){
                    System.out.println("D:\\tempfile\\"+fileNames[i]);
                    fis = new FileInputStream("D:\\tempfile\\"+fileNames[i]);
                    len = 0;
                    while ((len = fis.read(buffer)) != -1) {
                        fos.write(buffer, 0, len);//buffer从指定字节数组写入。buffer:数据中的起始偏移量,len:写入的字数。
                    }
                    fis.close();
                }
            }
            fos.flush();
            fos.close();
        }catch (IOException e){
            e.printStackTrace();
        } finally {
            System.out.println("合并完成！");
        }
    }
}
